package com.mjh.zhujianju.service.set.impl;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.google.common.collect.Lists;
import com.livew.framework.base.model.FileEO;
import com.livew.framework.base.service.FileService;
import com.mjh.zhujianju.util.FileUrlAdapter;
import com.mjh.zhujianju.vo.set.NavigationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.dao.set.NavigationMapper;
import com.mjh.zhujianju.model.set.NavigationEO;
import com.mjh.zhujianju.service.set.NavigationService;
import org.springframework.util.CollectionUtils;

@Service
public class NavigationServiceImpl extends BaseServiceImpl<NavigationEO> implements NavigationService{

	@Resource
	private NavigationMapper navigationMapper;

	@Autowired
	private FileService fileService;
	
	public NavigationServiceImpl(NavigationMapper navigationMapper) {
		super(navigationMapper);
	}
	
	

	  	@Override
	  	public void updateInfo(List<NavigationEO> list) throws XTBusinessException {
	  		if(!XTListUtils.isEmpty(list)) {
	  			for (NavigationEO navigationEO : list) {
				if(navigationEO.getId()!=null&&navigationEO.getStatus()!=null) {
					this.updateByPrimaryKeySelective(navigationEO);	
				}	
				}
	  		}
	  	}

	@Override
	public List<NavigationVO> selectNavigationList(String type) {
		List<NavigationVO> list = Lists.newArrayList();
		List<NavigationEO> navigationEOList = navigationMapper.selectNavigationList(type);
		if (CollectionUtils.isEmpty(navigationEOList)){
			return list;
		}

		NavigationVO vo;
		for (NavigationEO navigationEO : navigationEOList){
			vo = new NavigationVO();
			BeanUtils.copyProperties(navigationEO,vo);
			list.add(vo);
		}

		List<Long> ids = list.stream().map(NavigationVO -> NavigationVO.getId()).distinct().collect(Collectors.toList());
		List<FileEO> fileList = fileService.getFileByIds(ids,NavigationEO.class.getName(),null);
		if (CollectionUtils.isEmpty(fileList)){
			return list;
		}

		for (NavigationVO navigationVO : list){
			for (FileEO fileEO : fileList){
				if (navigationVO.getId().equals(fileEO.getCaseId())){
					navigationVO.setFileAction(FileUrlAdapter.assemblyAction(fileEO.getId()));
					continue;
				}
			}
		}
		return list;
	}
}
